package com.smalljj.comm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
	
    private static final String DB_NAME = "smalljj.db";

    private static final int DB_VERSION = 1;  
    
    private static final  String TBL_NAME = "position";
    
    private SQLiteDatabase db;
    
    public DBHelper(Context c) {
        super(c, DB_NAME, null, DB_VERSION);
    }
    
    @Override  
    public void onCreate(SQLiteDatabase db) {
        this.db = db;
		Log.i(Constants.LOG_TAG, "建表中..." );
        db.execSQL(" create table if not exists "  + TBL_NAME  + " (_id integer primary key autoincrement,createdate text,hour text, minute text,staytime text,longitude text,latitude text)");
        db.execSQL(" create index "+TBL_NAME+"_idx on "+TBL_NAME+"(createdate)");
        Log.i(Constants.LOG_TAG, "建表完成!" );
    }
    
    public void insert(ContentValues values) {
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TBL_NAME, null, values);
        db.close();
    }
    
    public Cursor query() {
        SQLiteDatabase db = getWritableDatabase();
        Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);
        return c;
    }
    
    /**
     * //查找gps信息 ,一段参考方法
	public List<String> listGPS(){
		List<String> list = new ArrayList<String>();
		
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor c = null;

		try
		{
			c = db.query(GPS_TABLE, null, null, null,
					null, null, null, null);
			while (c.moveToNext())
			{
				String longitude = c.getString(c.getColumnIndex("longitude"));
				String latitude = c.getString(c.getColumnIndex("latitude"));
				list.add(longitude+"-"+latitude);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			c.close();
			db.close();
		}
		
		return list;
	}
	*/
    
    public void del(int id) {
        if (db == null){
            db = getWritableDatabase();
        }
        db.delete(TBL_NAME, "_id=?", new String[] { String.valueOf(id) });
    }
    
    public void close() {
        if (db != null){
            db.close();
        }
    }
    
    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}